/*
 * @Author: 郭桓
 * @Date: 2019-02-14 14:15:40
 * @LastEditors: 郭桓
 * @LastEditTime: 2019-03-05 10:57:28
 * @Description: file content
 */
// Learn cc.Class:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        audio: {
            url: cc.AudioClip,
            default: null
        },
        btnMusic: cc.Node,
        _bgMusic: 0,
        _musicOn: true
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
    },

    onLoad() {
        this._bgMusic = cc.audioEngine.play(this.audio, true, 1);
        if (this.btnMusic != null) {
            this.btnMusic.runAction(cc.repeatForever(cc.rotateBy(2, 180)));
        }
        this._musicOn = true;
        let state = cc.sys.localStorage.getItem("musicstate"); //账号凭证 
        if (state == 'false' || state == false) {
            this._musicOn = false;
            if (this.btnMusic != null) {
                this.btnMusic.stopAllActions();
            }
        }
    },

    setButton(btn) {
        this.btnMusic = btn;
        this.btnMusic.stopAllActions();

        if (this._musicOn) {
            this.btnMusic.runAction(cc.repeatForever(cc.rotateBy(2, 180)));
        }
    },

    onSoundClick() {
        if (this._musicOn) {
            this._musicOn = false;
            this.btnMusic.stopAllActions();
            cc.audioEngine.pause(this._bgMusic);
        } else {
            this._musicOn = true;
            this.btnMusic.runAction(cc.repeatForever(cc.rotateBy(2, 180)));
            cc.audioEngine.resume(this._bgMusic);
        }
        cc.sys.localStorage.setItem("musicstate", this._musicOn); //账号凭证 
    },

    closeSound() {
        cc.audioEngine.stopAll();
    },

    start() {
        //判断是有对应的账号

        // this._utils = Utils.singleton;
        // if (Utils.singleton == null) {
        //     Utils.singleton = new Utils.Utils();
        //     this._utils = Utils.singleton;
        // }

        // // cc.sys.localStorage.setItem("hello", "value");
        // cc.log("cc.sys.localStorage.getItem() =" + cc.sys.localStorage.getItem("hello"));
        // var loginValue = cc.sys.localStorage.getItem("account"); //账号凭证 
        // if (loginValue == null || loginValue.length == 0) {
        //     //需要重新获取

        //     this._utils.request("")
        // }
    },

    // update (dt) {},
});